# -*- coding: utf-8 -*-
'''
Created on 2011-4-24

@author: cleo-home
'''
from django.forms.models import BaseModelFormSet
from models import Customer,Project,Employee,Commodity,Supplier,Store
from models import PurchaseRequest,PurchaseRequestItems 
from models import PurchaseOrderDetail,PurchaseOrder
from models import PurchaseReceipt,PurchaseReceiptDetail
from models import StockOut,StockOutItem
from models import Payment 
from django.forms import ModelForm, Textarea,ModelChoiceField,DateTimeField
from django.contrib.admin import widgets
from django.forms.extras.widgets import SelectDateWidget
from ModelLink import ModelLinkField,ModelLinkFieldNoLink
import logging

class PurchaseRequestForm_add(ModelForm):
    #PR_Customer = ModelChoiceField (Customer.objects.all(), label='客户',required=False,)
    #PR_Project = ModelChoiceField (Project.objects.all(), label='项目',required=False,)
    #PR_Adult_User = ModelChoiceField (Employee.objects.all(), label='指定审核人',)
    
    class Meta:
        model = PurchaseRequest
        fields = ('PR_Use', 'PR_Adult_User','PR_Customer','PR_Project','PR_Description')
        widgets = {
            'PR_Description': Textarea(attrs={'cols': 60, 'rows': 10}),
        }

class PurchaseRequestItemForm(ModelForm):
    #PRI_Commodity = ModelChoiceField (Commodity.objects.all(), label='物品',required=False,)
    #PRI_Quanlity = 
    #PR_Adult_User = ModelChoiceField (Employee.objects.all(), label='指定审核人',)
    
    class Meta:
        model = PurchaseRequestItems
        fields = ('PRI_Commodity', 'PRI_Price','PRI_Quanlity')
        widgets = {
            #'PR_Description': Textarea(attrs={'cols': 60, 'rows': 20}),
        }

class PurchaseOrderForm(ModelForm):
    """
        采购订单
    PO_ReceiptDateTime = models.DateTimeField("交货日期",null=True,blank=True,)
    //PO_DateTime = models.DateTimeField("制单日期",auto_now_add=True)
    //PO_Employee = models.ForeignKey(Employee,related_name="采购订单")   #采购员
    PO_Employee_Audit= models.ForeignKey(Employee,related_name="审核采购订单")   #采购员
    PO_Supplier = models.ForeignKey(Supplier,related_name="采购订单",null=True,blank=True,)   #供应商
    //PO_Ststus = models.CharField("状态",max_length=1, choices=PO_STATUS_CHOICES)
    PO_Description =  models.CharField("备注",max_length=200)
    //PO_PurchaseRequest = models.ForeignKey(PurchaseRequest,related_name="采购订单",null=True,blank=True,)
    //PO_LastModify_Date = models.DateTimeField("最后操作时间",blank=False,null=False,auto_now=True)
    """
    #PO_Supplier = ModelChoiceField (Supplier.objects.all(), label='供应商',required=False,)    
    #PO_Employee_Audit = ModelChoiceField (Employee.objects.all(), label='指定审核人',)    
    
    class Meta:
        model = PurchaseOrder
        fields = ('PO_Title','Project','PO_Supplier', 'PO_Employee_Audit','PO_Description','PO_ReceiptDateTime')
        widgets = {
            'PO_Description': Textarea(attrs={'cols': 60, 'rows': 10}),
        }

class PurchaseOrderDetailForm(ModelForm):
    #POD_Commodity = ModelChoiceField (Commodity.objects.all(), label='物品',required=False,)
    #PRI_Quanlity = 
    #PR_Adult_User = ModelChoiceField (Employee.objects.all(), label='指定审核人',)
    
    class Meta:
        model = PurchaseOrderDetail
        fields = ('POD_Commodity', 'POD_Price','POD_Quanlity')
        widgets = {
            #'PR_Description': Textarea(attrs={'cols': 60, 'rows': 20}),
        }

class PurchaseReceiptForm(ModelForm):
    '''
    PR_Title  = models.CharField("标题",max_length=50)
    PR_PurchaseOrder = models.ForeignKey(PurchaseOrder,related_name="采购收货单",null=True,blank=True,)
    PR_Supplier = models.ForeignKey(Supplier,related_name="采购收货单")
    PR_ReceiptDateTime = models.DateField("交货日期",null=True,blank=True,)
    PR_CreateDateTime = models.DateField("制单日期",auto_now_add=True)
    PR_CreateEmployee = models.ForeignKey(Employee,related_name="入库采购收货单")
    PR_ReceiptEmployee = models.ForeignKey(Employee,related_name="验收采购收货单",null=True,blank=True,)
    PR_Description = models.CharField("备注",max_length=500,null=True,blank=True,)
    PR_Ststus = models.CharField("状态",max_length=1, choices=Receipt_STATUS_CHOICES,default='I',blank=False, null=False)
    PR_LastModify_Date = models.DateTimeField("最后操作时间",blank=False,null=False,auto_now=True)
    
    '''
    #PR_Supplier = ModelChoiceField (Supplier.objects.all(), label='供应商',required=True,)    
    #PR_ReceiptEmployee = ModelChoiceField (Employee.objects.all(), label='指定验货人',)    
    
    class Meta:
        model = PurchaseReceipt
        fields = ('PR_Title','PR_Supplier', 'PR_ReceiptEmployee','PR_Description','PR_ReceiptDateTime')
        widgets = {
            'PR_Description': Textarea(attrs={'cols': 60, 'rows': 10}),
        }
    def __init__(self, *args, **kwargs):
        super(PurchaseReceiptForm, self).__init__(*args, **kwargs)
        #self.fields['mydate'].widget = widgets.AdminDateWidget()
        #self.fields['mytime'].widget = widgets.AdminTimeWidget()
        self.fields['PR_ReceiptDateTime'].widget =  SelectDateWidget()



class PurchaseReceiptDetailForm(ModelForm):
    """
        采购收货单明细（明细编号，采购收货编号，商品编码，库存数量）
        
    PurchaseReceipt = models.ForeignKey(PurchaseReceipt,related_name="采购收货单明细")
    Commodity = models.ForeignKey(Commodity,related_name="采购收货单明细")
    Quanlity = models.IntegerField("数量")
    Description = models.CharField("备注",max_length=50)
    """  
    #Commodity = ModelChoiceField (Commodity.objects.all(), label='物品',required=False,)
    #PRI_Quanlity = 
    #PR_Adult_User = ModelChoiceField (Employee.objects.all(), label='指定审核人',)
    
    class Meta:
        model = PurchaseReceiptDetail
        fields = ('Commodity', 'Quanlity','Description')
        widgets = {
            #'PR_Description': Textarea(attrs={'cols': 60, 'rows': 20}),
        }

class StockOutForm(ModelForm):
    '''     
    销售出库单（出库编号，客户编号，出库日期，备注，订单编号，制单人）    
    Title = models.CharField("标题",max_length=50)   
    Customer = models.ForeignKey(Customer,related_name="销售出库",null=True,blank=True,)#客户编号    
    CreateDateTime = models.DateTimeField("出库日期",auto_now_add=True)    
    Description = models.CharField("备注",max_length=500,null=True,blank=True,) 
    OrderID = models.CharField("订单编号",max_length=50,null=True,blank=True,)
    ApplyMan = models.ForeignKey(Employee,related_name="领用出库")
    AuditMan = models.ForeignKey(Employee,related_name="审核出库")
    StoreMan = models.ForeignKey(Employee,related_name="仓库出库",null=True,blank=True,)
    Ststus = models.CharField("状态",max_length=1, choices=Receipt_STATUS_CHOICES,default='I',blank=False, null=False)
    '''
    #Customer = ModelChoiceField (Customer.objects.all(), label='客户',required=False,)    
    #AuditMan = ModelChoiceField (Employee.objects.all(), label='指定审核人',)    
    
    class Meta:
        model = StockOut
        fields = ('Title','Customer', 'AuditMan','OrderID','Description')
        widgets = {
            'Description': Textarea(attrs={'cols': 60, 'rows': 10}),
        }
    def __init__(self, *args, **kwargs):
        super(StockOutForm, self).__init__(*args, **kwargs)

        #self.fields['PR_ReceiptDateTime'].widget =  SelectDateWidget()



class StockOutItemForm(ModelForm):
    """   
        销售出库单明细（明细编号，出库编号，商品编码，数量，金额）
     
    StockOut = models.ForeignKey(StockOut,related_name="销售出库单明细")
    Store = models.ForeignKey(Store,related_name="销售出库单明细")     #仓库编号
    Commodity = models.ForeignKey(Commodity,related_name="销售出库单明细")
    Quanlity = models.IntegerField("数量")
    Description = models.CharField("备注",max_length=50,null=True,blank=True,)
    """  
    #Commodity = ModelChoiceField (Commodity.objects.all(), label='物品',required=True,)
    #Store = ModelChoiceField (Store.objects.all(), label='仓库',required=True,)
    #PRI_Quanlity = 
    #PR_Adult_User = ModelChoiceField (Employee.objects.all(), label='指定审核人',)
    
    class Meta:
        model = StockOutItem
        fields = ('Store','Commodity', 'Quanlity','Description')
        widgets = {
            #'PR_Description': Textarea(attrs={'cols': 60, 'rows': 20}),
        }

class PaymentForm(ModelForm):
    '''     
     PurchaseOrder = models.ForeignKey(PurchaseOrder,verbose_name='订单',related_name="Payments",null=True,blank=True,)
    Customer = models.ForeignKey(Customer,verbose_name='客户',related_name="Payments",null=True,blank=True,)
    Title = models.CharField("标题",max_length=50)
    Amount = models.DecimalField("金额",max_digits=20,decimal_places=2,default=0)
    ApplyMan = models.ForeignKey(Employee,verbose_name='申请者',related_name="PaymentApply")
    AuditMan = models.ForeignKey(Employee,verbose_name='审核者',related_name="PaymentAudit")
    CreateDateTime = models.DateTimeField("创建日期",auto_now_add=True)
    Description = models.CharField("备注",max_length=50,null=True,blank=True,)
    PaymentSN = models.CharField("付款单号",max_length=50,null=True,blank=True,)
    Status = models.CharField("状态",max_length=1, choices=Payment_STATUS_CHOICES,default='I',blank=False, null=False)
    LastModify_Date = models.DateTimeField("最后操作时间",blank=False,null=False,auto_now=True)
       '''
    #Customer = ModelChoiceField (Customer.objects.all(), label='客户',required=False,)    
    #AuditMan = ModelChoiceField (Employee.objects.all(), label='指定审核人',)    
    
    class Meta:
        model = Payment
        fields = ('Title','Customer', 'PurchaseOrder',#'ApplyMan',
                  'AuditMan','Amount','PaymentSN','Description')
        widgets = {
            'Description': Textarea(attrs={'cols': 60, 'rows': 10}),
            #'ApplyMan':ModelLinkWidget(),
        }
    def __init__(self,  *args, **kwargs):
        super(PaymentForm, self).__init__(*args, **kwargs)
        #self.fields['PR_ReceiptDateTime'].widget =  SelectDateWidget()
        #logging.error(readonly)
#        if datareadonly :
#            
#            self.fields['PurchaseOrder'].widget =  ModelLinkField(self.instance.PurchaseOrder)
#             
#        if 'instance' in kwargs and kwargs['instance']!=None :
#            #logging.error(self.instance)           
#            self.fields['ApplyMan'].widget =  ModelLinkField(self.instance.ApplyMan)
#        else:            
#            del self.fields['ApplyMan']  
            

class PurchaseRequestFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(PurchaseRequestFormSet, self).__init__(*args, **kwargs)
        self.queryset = PurchaseRequest.objects.all()#filter(name__startswith='O')
        
class PurchaseRequestItemsForm(ModelForm):
    class Meta:
        model = PurchaseRequestItems